//
//  HomeViewController.m
//  VNExpressRSS
//
//  Created by Vinh Huynh on 8/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCell.h"
#import "VNExpressRssController.h"

@implementation HomeViewController
@synthesize cell;
@synthesize listNews;
@synthesize detailController;
@synthesize table;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
	[super viewDidLoad];	
	self.listNews = [[[NSMutableArray alloc] init] autorelease];
	NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Trang chủ - VNExpress",@"Name",@"http://vnexpress.net/rss/gl/trang-chu.rss",@"Link",nil];
	[self.listNews addObject:dict];
	[dict release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Xã Hội - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/xa-hoi.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Thế Giớ - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/the-gioi.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Kinh Doanh - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/kinh-doanh.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Văn Hóa - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/van-hoa.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Pháp Luật - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/phap-luat.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release]; 
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Đời Sống - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/doi-song.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Thể Thao - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/the-thao.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys: @"Khoa Học - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/khoa-hoc.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Vi Tính - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/vi-tinh.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Ôtô Xe Máy - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/oto-xe-may.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys: @"Bạn Đọc Viết - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/ban-doc-viet.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Tâm Sự - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/ban-doc-viet-tam-su.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
	
	dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Cười - VNExpress", @"Name", @"http://vnexpress.net/rss/gl/cuoi.rss", @"Link", nil];
	[self.listNews addObject:dict];
	[dict  release];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [listNews count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";    
    cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
	}
	NSDictionary *dict = [self.listNews objectAtIndex:[indexPath row]];
    cell.titleLabel.text = [dict objectForKey:@"Name"];    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 60;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	NSDictionary *dict = [self.listNews objectAtIndex:[indexPath row]];
	VNExpressRssController  *rssController = [[VNExpressRssController alloc] init];
     // ...
     // Pass the selected object to the new view controller.
	[self.navigationController pushViewController:rssController animated:YES];
	[rssController loadNewsWithRssLink:[dict objectForKey:@"Link"]];
	[rssController initTextSelected:[dict objectForKey:@"Name"]];
	[rssController release];	 
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	self.listNews = nil; 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[super dealloc];
	self.table = nil;
	self.detailController = nil;
	self.cell = nil;
	self.listNews = nil;
}

@end

